package com.star.star.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.star.dto.*;
import com.star.star.entity.Star;
import com.star.star.entity.StarChat;
import com.star.star.enums.ChatType;
import com.star.star.mapper.StarChatMapper;
import com.star.star.service.StarChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.star.service.StarService;
import com.star.star.vo.StarChatPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 粉丝留言艺人 服务实现类
 *
 * @author yangzj
 */
@Service
public class StarChatServiceImpl extends ServiceImpl<StarChatMapper, StarChat> implements StarChatService {

    @Autowired
    private StarService starService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void initiate(StarChatInitiateSaveDTO dto, Long userId) {
        Star star = Assert.notNull(starService.getById(dto.getStarId()), "艺人不存在");
        this.save(new StarChat().setType(ChatType.INITIATE.getCode())
                .setStarId(dto.getStarId()).setStarUserId(star.getUserId()).setUserId(userId)
                .setContent(dto.getContent()).setIsRead(false)
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void replay(StarChatReplaySaveDTO dto, Long userId) {
        Star star = Assert.notNull(starService.getOne(Wrappers.<Star>lambdaQuery().eq(Star::getUserId, userId)), "您还不是艺人");
        this.save(new StarChat().setType(ChatType.REPLAY.getCode())
                .setStarId(star.getId()).setStarUserId(userId).setUserId(dto.getUserId())
                .setContent(dto.getContent()).setIsRead(false)
        );
    }

    @Override
    public IPage<StarChatPageVO> pageFans(StarChatPageDTO dto) {
        return baseMapper.pageFans(dto);
    }

    @Override
    public IPage<StarChatPageVO> pageStar(StarChatPageDTO dto) {
        return baseMapper.pageStar(dto);
    }

    @Override
    public IPage<StarChatPageVO> pageReplay(StarChatReplayPageDTO dto) {
        IPage<StarChatPageVO> page = baseMapper.pageReplay(dto);
        // 艺人回复粉丝列表，已读粉丝留言
        List<Long> ids = page.getRecords().stream().filter(c -> c.getType().equals(ChatType.INITIATE.getCode()))
                .map(StarChatPageVO::getId).collect(Collectors.toList());
        this.updateBatchRead(ids);
        return page;
    }

    @Override
    public IPage<StarChatPageVO> pageInitiate(StarChatInitiatePageDTO dto) {
        IPage<StarChatPageVO> page = baseMapper.pageInitiate(dto);
        // 粉丝留言艺人列表，已读艺人回复
        List<Long> ids = page.getRecords().stream().filter(c -> c.getType().equals(ChatType.REPLAY.getCode()))
                .map(StarChatPageVO::getId).collect(Collectors.toList());
        this.updateBatchRead(ids);
        return page;
    }

    @Override
    public Integer getFansUnread(Long starId, Long userId) {
        return baseMapper.getFansUnread(starId, userId);
    }

    @Override
    public Integer getStarUnread(Long fansUserId, Long userId) {
        return null;
    }

    /**
     * 更新批量已读
     * 更新已读
     *
     * @param ids ids
     */
    private void updateBatchRead(List<Long> ids) {
        if (ids.isEmpty()) {
            return;
        }
        this.update(Wrappers.<StarChat>lambdaUpdate().in(StarChat::getId, ids)
                .set(StarChat::getIsRead, true));
    }
}
